package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.List;

public class InStorageOtherMaterielDetailActivity extends BaseActivity {

    private static final String TYPE_EDITTEXT = "1";

    private static final String TYPE_DATEPICKER = "2";

    private static final String TYPE_SELECT = "3";

    private static final String REQUIRED_NO = "0";

    private static final String REQUIRED_YES = "1";

    private static final String TYPE_MATERIEL_CODE = "materielCode";

    private static final String TYPE_INDEX = "index";

    private TextView title, materielCode, materielDescription, pageInfo, showAndHide;

    private EditText inStorageNumber;

    private Button previous, next;

    private String mMaterielCode;

    private int index;

    private InStorageOtherOverviewBean.MaterielListBean materielItem;

    private PercentLinearLayout batchLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_materiel_detail);
        initView(InStorageOtherMaterielDetailActivity.this);
        initData(InStorageOtherMaterielDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielCode = (TextView) activity.findViewById(R.id.materielCode);
        materielDescription = (TextView) activity.findViewById(R.id.materielDescription);
        inStorageNumber = (EditText) activity.findViewById(R.id.inStorageNumber);
        pageInfo = (TextView) activity.findViewById(R.id.pageInfo);
        showAndHide = (TextView) activity.findViewById(R.id.showAndHide);
        previous = (Button) activity.findViewById(R.id.previous);
        next = (Button) activity.findViewById(R.id.next);
        batchLayout = (PercentLinearLayout) activity.findViewById(R.id.batchLayout);
    }

    private void initData(BaseActivity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_materiel_detail_title));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_other_index_materiel_detail_material_list), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                backAndUpdate(activity);
            }
        });
        inStorageNumber.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            if (hasFocus) {
                inStorageNumber.setText("");
            }
        });
        previous.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (index > 0) {
                    index--;
                    changeAndUpdate(activity);
                } else {
                    backAndUpdate(activity);
                }
            }
        });
        next.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (index < (InStorageUtils.getOtherMaterielListSize(activity) - 1)) {
                    index++;
                    changeAndUpdate(activity);
                } else {
                    backAndUpdate(activity);
                }
            }
        });
        showAndHide.setOnClickListener((View v) -> {
            if (batchLayout.getVisibility() == View.GONE) {
                batchLayout.setVisibility(View.VISIBLE);
                ((TextView) v).setText(activity.getResources().getString(R.string.activity_in_storage_receive_materiel_detail_hide_information));
            } else if (batchLayout.getVisibility() == View.VISIBLE) {
                batchLayout.setVisibility(View.GONE);
                ((TextView) v).setText(activity.getResources().getString(R.string.activity_in_storage_receive_materiel_detail_show_information));
            }
        });
        getExtra(activity);
        getIndexByMaterielCode(activity, mMaterielCode);
        getData(activity, TYPE_MATERIEL_CODE);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            int indexTemp = InStorageUtils.getOtherIndexByMaterielCode(activity, mMaterielCode);
            if (indexTemp != -1) {
                if (indexTemp != index) {
                    index = indexTemp;
                    changeAndUpdate(activity);
                }
            } else {
                Utils.middleToast(activity, getResources().getString(R.string.activity_in_storage_receive_materiel_detail_not_find) + mMaterielCode + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_materiel));
            }
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private void getExtra(Activity activity) {
        mMaterielCode = activity.getIntent().getStringExtra(InStorageExtra.MATERIEL_CODE);
    }

    private void getIndexByMaterielCode(Context context, String materielCode) {
        index = InStorageUtils.getOtherIndexByMaterielCode(context, materielCode);
    }

    private void getData(BaseActivity context, String getMaterielInformationFromType) {
        switch (getMaterielInformationFromType) {
            case TYPE_MATERIEL_CODE:
                materielItem = InStorageUtils.getOtherMaterielInformationByMaterielCode(context, mMaterielCode);
                break;
            case TYPE_INDEX:
                materielItem = InStorageUtils.getOtherMaterielInformationByIndex(context, index);
                break;
        }
        materielCode.setText(materielItem.getMat_code());
        materielDescription.setText(materielItem.getMat_name());
        inStorageNumber.setText(TextUtils.isEmpty(materielItem.getInStorageNumber()) ? "" : (materielItem.getInStorageNumber() + materielItem.getName_zh()));
        batchLayout.removeAllViews();
        List<InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean> batchList = materielItem.getBatch_material_spec_list();
        for (int i = 0; i < batchList.size(); i++) {
            InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean batchItem = batchList.get(i);
            String type = batchItem.getBatch_spec_type();
            boolean required = REQUIRED_YES.equals(batchItem.getRequired()) ? true : false;
            switch (type) {
                case TYPE_EDITTEXT:
                    InStorageUtils.createEditText(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), i + 1);
                    break;
                case TYPE_DATEPICKER:
                    InStorageUtils.createDatePicker(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), i + 1);
                    break;
                case TYPE_SELECT:
                    InStorageUtils.createOtherSelect(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), batchItem.getInfo(), i + 1);
                    break;
            }
        }
        List<InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean> batchMaterialSpecList = materielItem.getBatch_spec_list();
        if (batchMaterialSpecList.size() != 0) {
            InStorageUtils.creatHorizontalView(context , batchLayout);
            for (int i = 0; i < batchMaterialSpecList.size(); i++) {
                InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean batchItem = batchMaterialSpecList.get(i);
                String type = batchItem.getBatch_spec_type();
                boolean required = REQUIRED_YES.equals(batchItem.getRequired()) ? true : false;
                switch (type) {
                    case TYPE_EDITTEXT:
                        InStorageUtils.createEditText(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), batchList.size() + i + 1);
                        break;
                    case TYPE_DATEPICKER:
                        InStorageUtils.createDatePicker(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), batchList.size() + i + 1);
                        break;
                    case TYPE_SELECT:
                        InStorageUtils.createOtherSelect(context, batchLayout, batchItem.getBatch_spec_name(), required, batchItem.getText(), batchItem.getBatch_spec_value(), batchItem.getInfo(), batchList.size() + i + 1);
                        break;
                }
            }
        }
        pageInfo.setText(InStorageUtils.getOtherMaterielListSize(context) + " | " + (index + 1));
        if (index <= 0) {
            previous.setText(getResources().getString(R.string.activity_in_storage_receive_materiel_detail_return));
        } else {
            previous.setText(getResources().getString(R.string.activity_in_storage_receive_materiel_detail_previous));
        }
        if (index >= (InStorageUtils.getOtherMaterielListSize(context) - 1)) {
            next.setText(getResources().getString(R.string.activity_in_storage_receive_materiel_detail_return));
        } else {
            next.setText(getResources().getString(R.string.activity_in_storage_receive_materiel_detail_next));
        }
    }

    private void updateInfo(Context context) {
        materielItem.setInStorageNumber(inStorageNumber.getText().toString().replace(materielItem.getName_zh(), "").trim());

        List<InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean> batchList = materielItem.getBatch_material_spec_list();
        for (int i = 0; i < batchList.size(); i++) {
            InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean batchItem = batchList.get(i);
            String type = batchItem.getBatch_spec_type();
            View view = findViewById(i + 1);
            switch (type) {
                case TYPE_EDITTEXT:
                    batchItem.setText(((EditText) view.findViewById(R.id.text)).getText().toString());
                    break;
                case TYPE_DATEPICKER:
                    batchItem.setText(((TextView) view.findViewById(R.id.text)).getText().toString());
                    break;
                case TYPE_SELECT:
                    batchItem.setBatch_spec_value(((TextView) view.findViewById(R.id.value)).getText().toString());
                    batchItem.setText(((TextView) view.findViewById(R.id.text)).getText().toString());
                    break;
            }
        }
        List<InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean> batchMaterialSpecList = materielItem.getBatch_spec_list();
        for (int i = 0; i < batchMaterialSpecList.size(); i++) {
            InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean batchItem = batchMaterialSpecList.get(i);
            String type = batchItem.getBatch_spec_type();
            View view = findViewById(batchList.size() + i + 1);
            switch (type) {
                case TYPE_EDITTEXT:
                    batchItem.setText(((EditText) view.findViewById(R.id.text)).getText().toString());
                    break;
                case TYPE_DATEPICKER:
                    batchItem.setText(((TextView) view.findViewById(R.id.text)).getText().toString());
                    break;
                case TYPE_SELECT:
                    batchItem.setBatch_spec_value(((TextView) view.findViewById(R.id.value)).getText().toString());
                    batchItem.setText(((TextView) view.findViewById(R.id.text)).getText().toString());
                    break;
            }
        }
        InStorageUtils.updateOtherInStorageItem(context, materielItem);
    }

    private String check(Context context) {
        String inStorageNumberStr = inStorageNumber.getText().toString().replace(materielItem.getName_zh(), "").trim();
        if (TextUtils.isEmpty(inStorageNumberStr)) {
            return getResources().getString(R.string.activity_in_storage_receive_materiel_detail_not_null);
        }

        if (!RegexUtils.checkDecimal(inStorageNumberStr)) {
            return getResources().getString(R.string.activity_in_storage_receive_materiel_detail_illegal);
        }

        if (!Utils.checkRetainDecimal(inStorageNumberStr, materielItem.getDecimal_place())) {
            return String.format(context.getResources().getString(R.string.common_retain_decimal_illegal), TextUtils.isEmpty(materielItem.getDecimal_place()) ? context.getResources().getString(R.string.common_retain_decimal_unlimited) : materielItem.getDecimal_place());
        }

        if (RegexUtils.checkZero(inStorageNumberStr)) {
            return getResources().getString(R.string.activity_in_storage_receive_materiel_detail_greater_than_zero);
        }

        List<InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean> batchList = materielItem.getBatch_material_spec_list();
        for (int i = 0; i < batchList.size(); i++) {
            InStorageOtherOverviewBean.MaterielListBean.BatchMaterialSpecListBean batchItem = batchList.get(i);
            String name = batchItem.getBatch_spec_name();
            String type = batchItem.getBatch_spec_type();
            String required = batchItem.getRequired();
            if (REQUIRED_YES.equals(required)) {
                View view = findViewById(i + 1);
                if (TYPE_EDITTEXT.equals(type)) {
                    EditText textEditText = (EditText) view.findViewById(R.id.text);
                    if ("".equals(textEditText.getText().toString())) {
                        return name + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_required_not_null);
                    }
                } else {
                    TextView textTextView = (TextView) view.findViewById(R.id.text);
                    if ("".equals(textTextView.getText().toString())) {
                        return name + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_required_not_null);
                    }
                }
            }
        }
        List<InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean> batchMaterialSpecList = materielItem.getBatch_spec_list();
        for (int i = 0; i < batchMaterialSpecList.size(); i++) {
            InStorageOtherOverviewBean.MaterielListBean.BatchSpecListBean batchItem = batchMaterialSpecList.get(i);
            String name = batchItem.getBatch_spec_name();
            String type = batchItem.getBatch_spec_type();
            String required = batchItem.getRequired();
            if (REQUIRED_YES.equals(required)) {
                View view = findViewById(batchList.size() + i + 1);
                if (TYPE_EDITTEXT.equals(type)) {
                    EditText textEditText = (EditText) view.findViewById(R.id.text);
                    if ("".equals(textEditText.getText().toString())) {
                        return name + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_required_not_null);
                    }
                } else {
                    TextView textTextView = (TextView) view.findViewById(R.id.text);
                    if ("".equals(textTextView.getText().toString())) {
                        return name + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_required_not_null);
                    }
                }
            }
        }
        return null;
    }

    private void backAndUpdate(Context context) {
        String checkResult = check(context);
        if (checkResult == null) {
            updateInfo(context);
            super.onBackPressed();
        } else {
            DialogUtils.getConfirmDialog(context, checkResult + "\n" + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_force_exit), new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    InStorageOtherMaterielDetailActivity.super.onBackPressed();
                }
            }).show();
        }
    }

    private void changeAndUpdate(BaseActivity activity) {
        String checkResult = check(activity);
        if (checkResult == null) {
            updateInfo(activity);
            getData(activity, TYPE_INDEX);
        } else {
            DialogUtils.getConfirmDialog(activity, checkResult + "\n" + getResources().getString(R.string.activity_in_storage_receive_materiel_detail_force_switch), new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    dialog.dismiss();
                    getData(activity, TYPE_INDEX);
                }
            }).show();
        }
    }

    @Override
    public void onBackPressed() {
        backAndUpdate(InStorageOtherMaterielDetailActivity.this);
    }
}
